<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/11 0011
 * Time: 下午 5:19
 * Administrator
 */

namespace app\api\controller;


use app\common\Enum\CommonEnum;
use app\common\model\Articles;
use app\common\model\SysLogLogin;
use app\common\model\UserEquipments;
use app\common\model\Users;
use app\common\service\DateService;
use app\common\service\SysConfigService;
use app\common\util\GetuiUtil;
use app\common\util\ReturnMsgUtil;
use think\Controller;

class Index extends Controller
{
    use ReturnMsgUtil,GetuiUtil;
    public function getUserInfo()
    {
        $uid = input('user_id');
        $cid = input('clientId');
        if (!$uid){
            return $this->errorInfo('您没有SIM卡，无法运行此程序',999);
        }
        $ueM = new UserEquipments();
        $countUe = $ueM -> count('*');
        if ($countUe > SysConfigService::getSysConfig('app.first.count.user')){
            return $this->errorInfo('用户量已满，请等待下次开放时间',999);
        }
        $ue = $ueM::get(['equipment_id'=>$uid]);
        if (!$ue){
            $user = Users::create([
                'groupId'=>'4'
                ,'username'=>''
                ,'email'=>''
                ,'password'=>''
                ,'nickname'=>''
                ,'personalitySignature'=>''
                ,'trueName'=>''
                ,'idCardNo'=>''
            ]);
            $ue = UserEquipments::create([
                'user_id'=>$user->userId
                ,'equipment_id'=>$uid
                ,'Getui_client_id'=>$cid
                ,'status'=>CommonEnum::USER_EQUIPMENTS_VALID
            ]);
        }
        $user = Users::get($ue->user_id);
        if (!$ue->Getui_client_id){
            $ue->save(['Getui_client_id'=>$cid]);
        }
        $sll = SysLogLogin::get([
            'user_equipments_id'=>$ue->id
        ]);
        if (!$sll) {
            SysLogLogin::create([
                'user_id'=>$user->userId
                ,'user_equipments_id' => $ue->id
                , 'phone_model' => input('model')
                , 'positionX' => input('pX')
                , 'positionY' => input('pY')
                ,'loginTime'=>date(DateService::DATE_FORMART_STRING,$_SERVER['REQUEST_TIME'])
            ]);
        }else{
            $sll->save([
                'positionX' => input('pX')
                ,'positionY' => input('pY')
                ,'loginTime'=>date(DateService::DATE_FORMART_STRING,$_SERVER['REQUEST_TIME'])
            ]);
        }
        $result = array_merge($ue->append(['user_banner_img_url_host'])->toArray(),$user->toArray());
        return $this->successInfo('成功',$result);
    }

    public function getAllConfig()
    {
        return $this->successInfo('成功',SysConfigService::getAppBaseConfigs());
    }

    public function index()
    {
        $arts = Articles::all(['type'=>'1000','status'=>99]);
        if (!$arts){
            return $this->errorInfo('暂无消息');
        }
        return $this->successInfo('成功获取消息',urlencode(urlencode(json_encode($arts))));
    }
}